﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.ZGCG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 中国成果操作对象
    /// </summary>
    private ZGCG_DAO zgcgDAO = new ZGCG_DAO();
    /// <summary>
    /// 日志操作对象
    /// </summary>
    private OperationLog_DAO operationLogDAO = new OperationLog_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        String message = null;
        String id = HttpUtility.UrlDecode(Request["id"]);
        String zyfl = HttpUtility.UrlDecode(Request["zyfl"]);
        int ID = 0;

        if (null == id || "".Equals(id))
        {
            message = "错误：编号参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            try
            {
                ID = Int32.Parse(id);
            }
            catch (Exception)
            {
            }
        }

        //指定成果代码
        ZGCG_PM zgcgPM = this.zgcgDAO.GetZGCG_PMByID(ID);

        if (null == zgcgPM)
        {
            message = "错误：没有找到该中国成果";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        /*TODO:改动地方
        //注意：一级分类的时候zyfl为空，专业分类不够详细
        if (null == zyfl || "".Equals(zyfl))
        {
            message = "错误：专业分类参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        */
        //重新获得分类信息,注意flbm中含有多个编码值
        String flbm = zgcgPM.flbm.Split(';')[0];
        String fl = "";
        for (int i = 2; i < flbm.Length; i++)
        {
            if (i % 2 == 0) fl = fl + flbm[i];
            if (fl.Length == 5) fl = fl + ".";
        }
        //如果没有匹配到合适的分类，采用大分类
        if ("".Equals(fl)) fl = zyfl;

        ZGCG_FL zgcgFL = null;
        int len = 0;
        while (zgcgFL == null&&len<fl.Length)
        {
            zgcgFL = this.zgcgDAO.GetZGCG_FLByZyfl(fl.Substring(0, fl.Length - len));
            len++;
        }

        User user = (User)Session["login"];
        //登陆超时则退到登录页
        if (null == user)
        {
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }

        //设置返回按钮链接,TODO:返回不成功
        this.backHyperLink.NavigateUrl = GlobalKJCG.LISTPM_PAGE + "?catalog=" + zyfl;
 
        //自动编号
        this.idHiddenField.Value = ID.ToString();
       
        //专业分类
        this.zyflTextBox.Text = zgcgFL.zyfl;
        //分类名称
        this.flmcTextBox.Text = zgcgFL.flmc;
        //分类编码
        this.flbmTextBox.Text= zgcgPM.flbm;
        //发布年份
        this.fbnfTextBox.Text = zgcgPM.fbnf.ToString();
        //成果编号
        this.cgbhTextBox.Text = zgcgPM.cgbh;

        //成果名称
        this.cgmcTextBox.Text = zgcgPM.cgmc;
        //完成单位
        this.wcdwTextBox.Text = zgcgPM.wcdw;
        //完成人
        this.wcrTextBox.Text = zgcgPM.wcr;
        //中文主题词
        this.zwztcTextBox.Text = zgcgPM.zwztc;
        this.dwdzTextBox.Text = zgcgPM.dwdz;//单位地址
        this.yzbmTextBox.Text = zgcgPM.yzbm;//邮政编码
        this.dwdhTextBox.Text = zgcgPM.dwdh;//单位电话
        //成果简介
        this.cgjjTextBox.Text = zgcgPM.cgjj;
        //研究起止时间
        this.yjqzsjTextBox.Text = zgcgPM.yjqzsj;
        this.gtflTextBox.Text = zgcgPM.gtfl;//国图分类
        this.cglbTextBox.Text = zgcgPM.cglb;//成果类别
        this.cgspTextBox.Text = zgcgPM.cgsp;//成果水平
        this.jdbmTextBox.Text = zgcgPM.jdbm;//鉴定部门
        this.jdrqTextBox.Text = zgcgPM.jdrq;//鉴定日期
        this.lxhhjTextBox.Text = zgcgPM.lxhhj;//立项或获奖
        this.fbdwTextBox.Text = zgcgPM.fbdw;//发布单位
        this.zllyTextBox.Text = zgcgPM.zlly;//资料来源
        this.lybhTextBox.Text = zgcgPM.lybh;//来源编号
        this.hydmTextBox.Text = zgcgPM.hydm; //行业代码
        this.hymcTextBox.Text = zgcgPM.hymc; //行业名称

        this.zlsqTextBox.Text = zgcgPM.zysq;//专利授权
        this.kfCheckBox.Checked = zgcgPM.kf;//开放
   }

    /// <summary>
    /// 获取用户填写的中国成果篇名信息
    /// </summary>
    /// <returns></returns>
    private ZGCG_PM GetZGCGPM()
    {
        ZGCG_PM zgcgPM = new ZGCG_PM();
        //自动编号
        zgcgPM.id = Int32.Parse(this.idHiddenField.Value);
        //成果编号
        zgcgPM.cgbh = this.cgbhTextBox.Text;
        //分类编码
        zgcgPM.flbm = this.flbmTextBox.Text;
        //成果名称
        zgcgPM.cgmc = this.cgmcTextBox.Text;
        //完成单位
        zgcgPM.wcdw = this.wcdwTextBox.Text.Trim();
        //完成人
        zgcgPM.wcr = this.wcrTextBox.Text.Trim();
        //单位地址
        zgcgPM.dwdz = this.dwdzTextBox.Text.Trim();
        //邮政编码
        zgcgPM.yzbm = this.yzbmTextBox.Text;
        //单位电话
        zgcgPM.dwdh = this.dwdhTextBox.Text;
        //中文主题词
        zgcgPM.zwztc = this.zwztcTextBox.Text;
        //成果简介
        zgcgPM.cgjj = this.cgjjTextBox.Text;
        zgcgPM.yjqzsj = this.yjqzsjTextBox.Text;//研究起止时间
        zgcgPM.gtfl = this.gtflTextBox.Text;//国图分类
        zgcgPM.cglb = this.cglbTextBox.Text;//成果类别
        zgcgPM.cgsp = this.cgspTextBox.Text;//成果水平

        zgcgPM.jdbm = this.jdbmTextBox.Text;//鉴定部门
        zgcgPM.jdrq = this.jdrqTextBox.Text;//鉴定日期
        zgcgPM.lxhhj = this.lxhhjTextBox.Text;//立项或获奖
        zgcgPM.fbdw = this.fbdwTextBox.Text;//发布单位
        zgcgPM.zlly = this.zllyTextBox.Text;//资料来源
        zgcgPM.fbnf = Int32.Parse(this.fbnfTextBox.Text);//发布年份
        zgcgPM.lybh = this.lybhTextBox.Text;//来源编号
        zgcgPM.hydm = this.hydmTextBox.Text;//行业代码
        zgcgPM.hymc = this.hymcTextBox.Text;//行业名称
        zgcgPM.zysq = this.zlsqTextBox.Text;//专利授权
        zgcgPM.kf = this.kfCheckBox.Checked;//开放
        zgcgPM.gxrq = DateTime.Now;//更新日期

        return zgcgPM;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //验证成果编号
        if (!this.ValidateCGBH())
        {
            return;
        }

        //验证成果名称
        if (!this.ValidateCGMC())
        {
            return;
        }

        //验证中文主题词
        if (!this.ValidateZWZTC())
        {
            return;
        }

        ZGCG_PM zgcgPM = this.GetZGCGPM();
        if (this.zgcgDAO.UpdateZGCG_PM(zgcgPM, ((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('更新中国成果信息成功')</Script>");
            this.BindData();
        }
        else
        {
            Response.Write("<Script>alert('更新中国成果信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证成果编号
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCGBH()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (this.cgbhTextBox.Text.Split('-')[1] != this.fbnfTextBox.Text)
        {
            this.errorLabel.Text = "错误：成果编号中的年份与发布年份不符.";
            flag = false;
        }
        return flag;
    }

    ///<summary>验证中文主题词</summary>
    private Boolean ValidateZWZTC()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";

        if (null == this.zwztcTextBox.Text || "".Equals(this.zwztcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误: 中文主题词不能为空";
            flag = false;
        }
        else
        {
            String[] p = this.zwztcTextBox.Text.Split(';');
            if (p.Length < 3)
            {
                this.errorLabel.Text = "错误: 中文主题词不能少于3个";
                flag = false;
            }
        }

        return flag;
    }

    /// <summary>
    /// 验证成果名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCGMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.cgmcTextBox.Text||"".Equals(this.cgmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：中文名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>验证日期</summary>
    private Boolean ValidateRQ(String rq, String msg)
    {
        this.errorLabel.Text = "";

        Boolean flag = true;
        if (rq != "")
        {
            try
            {
                DateTime val = DateTime.Parse(rq);
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：" + msg + "格式不正确(yyyy-MM-dd).";
                return false;
            }
        }
        return flag;
    }
   
    //全角转半角
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    protected void gtflTextBox_TextChanged(object sender, EventArgs e)
    {
        this.gtflTextBox.Text = GlobalMethod.Semicolon(this.gtflTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void hymcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.hymcTextBox.Text = GlobalMethod.Semicolon(this.hymcTextBox.Text.Trim());
    }
    protected void hydmTextBox_TextChanged(object sender, EventArgs e)
    {
        this.hydmTextBox.Text = GlobalMethod.Semicolon(this.hydmTextBox.Text.Trim());
    }
    protected void wcrTextBox_TextChanged(object sender, EventArgs e)
    {
        this.wcrTextBox.Text = GlobalMethod.Semicolon(this.wcrTextBox.Text.Trim());
    }
    protected void flbmTextBox_TextChanged(object sender, EventArgs e)
    {
        this.flbmTextBox.Text = GlobalMethod.Semicolon(this.flbmTextBox.Text.Trim());
    }
}